(function() {
    'use strict';
    angular.module('weApp.controllers')
            //公告栏
            .controller('boardCtrl', ['$scope', '$rootScope', '$state', '$stateParams', '$timeout', '$http', 'weAppDataBase', 'showLinePageService', 'localStorageService', 'myFavorsService', '$ionicLoading',
                function($scope, $rootScope, $state, $stateParams, $timeout, $http, weAppDataBase, showLinePageService, localStorageService, myFavorsService, $ionicLoading) {
                    var look = localStorageService.get("noticeId") == null ? [] : JSON.parse(localStorageService.get("noticeId"));
                    var rootR = {
                        method: 'post',
                        url: $rootScope.rootLocation + '/line/bus/notices',
                        data: {
                            "cityName": $rootScope.cityName,
                            "cityCode": $rootScope.cityCode
                        }
                    };
                    if (!$rootScope.board) {
                        $ionicLoading.show({
                            template: '读取中...'
                        }).then(function() {
                            $http(rootR).then(function(boards) {
                                $ionicLoading.hide();
                                if (boards.data.head.success == "true") {
                                    var board = boards.data.data.notices;

                                    for (var i in board) {
                                        if (i >= 0) {
                                            board[i].noticeContents = board[i].noticeContent.substr(0, 60) + '...';
                                            board[i].noticeContentd = board[i].noticeContent.replace(/\n/g, "<br/>")
                                        }
                                        if (look.length > 0) {
                                            if (look.indexOf(board[i].noticeId) >= 0) {
                                                board[i].tag = 1;
                                            } else {
                                                board[i].tag = 0;
                                            }
                                        }
                                    }
                                    $rootScope.board = board;
                                }

                            });
                        });
                    } else {
                        var board = $rootScope.board;
                        for (var i in board) {
                            if (i >= 0) {
                                board[i].noticeContents = board[i].noticeContent.substr(0, 60) + '...';
                                board[i].noticeContentd = board[i].noticeContent.replace(/\n/g, "<br/>")
                            }
                            if (look.length > 0) {
                                if (look.indexOf(board[i].noticeId) >= 0) {
                                    board[i].tag = 1;
                                } else {
                                    board[i].tag = 0;
                                }
                            }
                        }
                        $rootScope.board = board;
                    }


                    //下拉刷新
                    $rootScope.doRefresh = function() {
                        $http(rootR).then(function(boards) {
                            if (boards.data.head.success == "true") {
                                var board = boards.data.data.notices;
                                for (var i in board) {
                                    if (i >= 0) {
                                        board[i].noticeContents = board[i].noticeContent.substr(0, 60) + '...';
                                        board[i].noticeContentd = board[i].noticeContent.replace(/\n/g, "<br/>")
                                    }
                                    if (look.length > 0) {
                                        if (look.indexOf(board[i].noticeId) >= 0) {
                                            board[i].tag = 1;
                                        } else {
                                            board[i].tag = 0;
                                        }
                                    }
                                }
                                $rootScope.board = board;
                            }

                        })
                                .finally(function() {
                                    $scope.$broadcast('scroll.refreshComplete');
                                });

                    }

                    $rootScope.details = function(title, content, id) {
                        $rootScope.title = title;
                        $rootScope.content = content;
                        localStorageService.set("noticeTit", title);
                        localStorageService.set("noticeCon", content);
                        var localTran = localStorageService.get("noticeId") == null ? [] : JSON.parse(localStorageService.get("noticeId"));
                        // str = start + '@*@' + end;
                        if (localTran.indexOf(id) == -1) {
                            localTran.push(id);
                            localStorageService.set("noticeId", JSON.stringify(localTran));
                        }
                    }


                }]);
})();